package com.wenli.graduate2022.wenliGreenShop.repository.mapper;

import com.wenli.graduate2022.wenliGreenShop.entity.ProductOrder;
import com.wenli.graduate2022.wenliGreenShop.entity.ProductOrderItem;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author HuangHai
 * @date 2021/10/13 15:10
 */
@Repository
public interface ProductOrderItemMapper {

    @Results(id = "productOrderItem",value= {
            @Result(property = "productOrderItemProduct", column = "productorderitem_product_id",
                    one = @One(select = "com.wenli.graduate2022.wenliGreenShop.repository.mapper.ProductMapper.findById",
                            fetchType = FetchType.LAZY)),
            @Result(property = "productOrderItemUser", column = "productorderitem_user_id",
                    one = @One(select = "com.wenli.graduate2022.wenliGreenShop.repository.mapper.UserMapper.findById",
                            fetchType = FetchType.LAZY))
    })
    @Select("select * from productOrderItem where productorderitem_product_id = #{productId}")
    List<ProductOrderItem> findByProductId(Integer productId);    //根据产品id查询所有产品订单，可以用于查询该产品已经销售了多少件

    @Options(useGeneratedKeys = true, keyProperty = "productorderitemId",keyColumn = "productorderitem_id")
    @Insert("insert into productOrder(productorderitem_number,productorderitem_price,productorderitem_product_id,productorderitem_order_id," +
            "productorderitem_user_id,productorderitem_userMessage" +
            " values(#{productorderitemNumber},#{productorderitemPrice},#{productId},#{productorderitemOrderId},#{userId}," +
            "#{productorderitemUserMessage}")
    @ResultMap("productOrderItem")
    int insert(ProductOrderItem productOrderItem, String productId, String userId);

    @Select("select * from productOrderItem where productorderitem_order_id = #{id}")
    @ResultMap("productOrderItem")
    List<ProductOrderItem> findByOrderId(Integer id);  // 根据订单id查询该订单下的所有产品订单


    @Select("select * from productOrderItem where productorderitem_id = #{id}")
    @ResultMap("productOrderItem")
    ProductOrderItem findById(Integer id);        //根据id查询产品订单


    @Select("select * from productOrderItem where productorderitem_user_id = #{id}")
    @ResultMap("productOrderItem")
    List<ProductOrderItem> findByUserId(Integer id);  //根据用户id查询该用户下的所有产品订单




}
